<!-- Danh sách sẽ có phân trang -->
<?php
	include_once "../classes/nguoidung_bs.php";
	include_once "../classes/nguoidungloai_bs.php";
	include_once "../classes/thuvienham.php";
	session_start();
	PhanQuyenAdmin();
	$Keyword = trim($_GET['key']);
	$Page    = trim($_GET['pag']);
	
	$NguoiDung = new nguoidung_bs();
	if(trim($Keyword) != "")
		$NguoiDung->SetHoTen($Keyword);
	$DNguoiDung = $NguoiDung->LayDanhSach();
	
	$Limit = 20; //Element Per Page
	$SoTrang = ceil(count($DNguoiDung) / $Limit);
	
	$DNguoiDung = $NguoiDung->LayDanhSachPaging(($Page - 1) * $Limit, $Limit);
?>
<div class="Paging">
	<?php
		if($DNguoiDung != null)
		{
			if($Page > 1)
				echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLNguoiDung(\"$Keyword\",1);'>Đầu</a>";	
			for($i = 1; $i <= $SoTrang; $i++)
			{
				if($Page - 5 <= $i && $i <= $Page + 5)
				{
					if($i == $Page)
						echo "<a class='AjaxPageCur' href='#' onclick='javascript:loadXMLNguoiDung(\"$Keyword\",$i);'>$i</a>";
					else
						echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLNguoiDung(\"$Keyword\",$i);'>$i</a>";
				}
				elseif($i == $Page + 6)
					echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLNguoiDung(\"$Keyword\",$i);'>...</a>";
				elseif($i == $Page - 6)
					echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLNguoiDung(\"$Keyword\",$i);'>...</a>";
			}
			if($Page < $SoTrang)
				echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLNguoiDung(\"$Keyword\",$SoTrang);'>Cuối</a>";	
			
			echo "<span style='float:right; background-color:#4ABDF4; color: #FFF; padding:0px 3px'>Trang $Page/$SoTrang</span>";
		}
	?>
</div>
<div class="TableList">
	
	<div id="result">
		<table width="100%" class="DSHD">
			<tr>
				<th rowspan="2" class="first">STT</th>
				<th rowspan="2" width="100px">Tên <br /> đăng nhập</th>
				<th rowspan="2" colspan="2">Họ tên</th>
				<th rowspan="2">Email</th>
				<th rowspan="2">Điện thoại</th>
				<th rowspan="2" colspan="3">Vai trò</th>
				<th colspan="4" class="ColSpan">Tùy chọn khác</th>
			</tr>
			<tr>
				<th class="Op">Chi tiết</th>
				<th class="Op">Chỉnh sửa</th>
				<th class="Op">Xóa</th>
				<th class="Op">Đặt lại MK</th>
			</tr>
			<!-- Lấy danh sách học viên từ cơ sở dữ liệu -->
			<?php
				if($DNguoiDung == null)
				{
					echo '<tr><td colspan="11" align="center"> Nội dung rỗng</td></tr>';
				}
				else
				{
					$stt = 0;
					for($i = 0; $i < count($DNguoiDung); $i++)
					{
						if($DNguoiDung[$i]['MaNguoiDung'] == 'admin' || $DNguoiDung[$i]['MaNguoiDung'] == $_SESSION['USER']['MaNguoiDung'])
						{
							continue;
						}
						$NguoiDungLoai = new nguoidungloai_bs();
						$NguoiDungLoai->SetMaLoaiNguoiDung($DNguoiDung[$i]['MaLoaiNguoiDung']);
						$DNguoiDungLoai = $NguoiDungLoai->LayDanhSach();
						$TenLoai = $DNguoiDungLoai[0]['TenLoaiNguoiDung'];
						$CapQuyen = 0; //Đang ở quyền thấp hơn
						if($DNguoiDung[$i]['MaLoaiNguoiDung'] == 'US1')
						{
							$TenLoai = '<b>' . $DNguoiDungLoai[0]['TenLoaiNguoiDung'] . '</b>';
							$CapQuyen = 1;
						}
						$HT = TachHoTen($DNguoiDung[$i]['HoTen']);
						$LinkSetRole = 'index.php?act=nguoidung_thaydoiquyen&id=' . $DNguoiDung[$i]['MaNguoiDung'] . '&pin=' . $DNguoiDung[$i]['PIN'];
						$LinkDetails = 'index.php?act=nguoidung_chitiet&id=' . $DNguoiDung[$i]['MaNguoiDung'] . '&pin=' . $DNguoiDung[$i]['PIN'];
						$LinkEdit = 'index.php?act=nguoidung_capnhat&id=' . $DNguoiDung[$i]['MaNguoiDung'] . '&pin=' . $DNguoiDung[$i]['PIN'];
						$LinkTrash = 'index.php?act=nguoidung_xoa_sub&id=' . $DNguoiDung[$i]['MaNguoiDung'] . '&pin=' . $DNguoiDung[$i]['PIN'];
						$LinkResetPass = 'index.php?act=nguoidung_datlaimatkhau&id=' . $DNguoiDung[$i]['MaNguoiDung'] . '&pin=' . $DNguoiDung[$i]['PIN'];
						
						echo '<tr class="ButtonDetails">';
							echo '<td align="center"><b>' . (($Page - 1) * $Limit + ($stt + 1)) . '</b></td>';
							echo '<td align="center">' . $DNguoiDung[$i]['MaNguoiDung'] . '</td>';
							echo '<td>' . $HT[0] . '</td>';
							echo '<td>' . $HT[1] . '</td>';
							echo '<td>' . $DNguoiDung[$i]['Email'] . '</td>';
							echo '<td>' . $DNguoiDung[$i]['DienThoai'] . '</td>';
							
							echo '<td align="center">' . ($CapQuyen == 1 ? '<a title="Giảm quyền" href="' . $LinkSetRole . '"><img src="../data/godown.png" alt="" /></a>' : '') . '</td>';
							echo '<td align="center">' . $TenLoai . '</td>';
							echo '<td align="center">' . ($CapQuyen == 0 ? '<a title="Nâng quyền" href="' . $LinkSetRole . '"><img src="../data/goup.png" alt="" /></a>' : '') . '</td>';
							
							echo '<td align="center"><a href="' . $LinkDetails . '" ><img src="../data/details.png" alt="" /></a></td>';
							echo '<td align="center"><a href="' . $LinkEdit . '" ><img src="../data/edit.png" alt="" /></a></td>';
							echo '<td align="center"><a onclick="return confirm(\'Bạn muốn xóa người dùng?\');" href="' . $LinkTrash . '" ><img src="../data/delete.png" alt="" /></a></td>';echo '<td align="center"><a  href="' . $LinkResetPass . '" ><img height="16px" src="../data/menu_changepass.png" alt="" /></a></td>';
						echo '</tr>';
						$stt++;
					}
				}
			?>
		</table>
	</div>
	
</div>